<?php

namespace App\Jobs;

use App\Goods;
use App\GroupBuyItem;
use App\GroupMaster;
use App\Order;
use App\OrderDetail;
use App\Services\RefundService;
use App\Sku;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CancelGroup implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $group_master_id;
    public $retryAfter = 3;
    /**
     * Create a new job instance.
     *
     * @param $group_master_id
     */
    public function __construct($group_master_id)
    {
        $this->group_master_id=$group_master_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $group_master=GroupMaster::find($this->group_master_id);

            if ($group_master->over==0){
                if ($group_master->current_num<$group_master->min_man){
                    $group_master->over=-1;
                    $group_master->save();
                     GroupBuyItem::where('group_master_id',$this->group_master_id)->update(['status'=>-1]);
                    $buy_items=GroupBuyItem::where('group_master_id',$this->group_master_id)->get();
                    $refound=new RefundService();
                    foreach ($buy_items as $key=>$value){
                       $refund_number=$refound->refund($value['order_number'],"团购取消");
                       if ($refund_number){
                           $data=[
                               'refund_number'=>$refund_number,
                               'status'=>-3,
                               'group_status'=>-1
                           ];
                           Order::where('order_number',$value['order_number'])->update($data);
                           $details=OrderDetail::where('order_id',$value['order_number'])->get();
                           foreach ($details as $k=>$v){
                               if ($v['sku_id']){
                                   Sku::where('id',$v['sku_id'])->increment('stock',$v['number']);
                               }else{
                                   Goods::where('id',$v['goods_id'])->increment('stock',$v['number']);
                               }
                           }

                       }
                       //退库存

                    }
                    //退款操作


                }elseif ($group_master->current_num>=$group_master->min_man){
                    $group_master->over=1;
                    $group_master->save();
                    $buy_items=GroupBuyItem::where('group_master_id',$this->group_master_id)->get();
                    foreach ($buy_items as $key=>$value){


                            $data=[

                                'group_status'=>1
                            ];
                            Order::where('order_number',$value['order_number'])->update($data);

                    }
                }
            }


    }
    private function refund()
    {

    }


}
